/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.utilities;

import java.util.Iterator;
import java.util.Map.Entry;

/**
 *
 * @author Administrator
 */
public class QueryBuilder {

    public static final String ASC = "asc";
    public static final String DESC = "desc";
    public static final String WHERE = " where ";
    public static final String ORDER_BY = " order by ";
    public static final String AND = " and ";
    public static final String SPACE = " ";
    public static final String EQUALS = " = ";
    public static final String LIKE = " like ";
    public static final String ANYTHING = "%";
    public static final String FROM = " from ";

    public static String buildQuery(QueryElements queryElements) {
        String queryString = "";

        queryString += QueryBuilder.FROM
                + queryElements.getClassName()
                + QueryBuilder.SPACE
                + queryElements.getClassVariable();

        if (queryElements.getSearchAttribute() != null) {
            if (!queryElements.getSearchAttribute().isEmpty()) {
                queryString += QueryBuilder.WHERE
                        + queryElements.getClassVariable()
                        + "."
                        + queryElements.getSearchAttribute()
                        + QueryBuilder.LIKE
                        + "'"
                        + QueryBuilder.ANYTHING
                        + queryElements.getSearchValue()
                        + QueryBuilder.ANYTHING
                        + "'";
            }
        }

        if (!queryElements.getAttributes().isEmpty()) {
            if (queryElements.getSearchAttribute() != null) {
                queryString += QueryBuilder.AND;
            } else {
                queryString += QueryBuilder.WHERE;
            }

            for (int i = 0; i < queryElements.getAttributes().size(); i++) {
                String attributeName = queryElements.getAttributes().get(i);
                String variableName = queryElements.getVariables().get(i);
                queryString += queryElements.getClassVariable() + "." + attributeName + " = :" + variableName;
                queryString += QueryBuilder.AND;
            }

            queryString = queryString.substring(0, queryString.length() - 5);
        }

        if (!queryElements.getStrictAttributes().isEmpty()) {
            if (queryElements.getSearchAttribute() != null || !queryElements.getAttributes().isEmpty()) {
                queryString += QueryBuilder.AND;
            } else {
                queryString += QueryBuilder.WHERE;
            }
            for (String s : queryElements.getStrictAttributes()) {
                queryString += s;
                queryString += QueryBuilder.AND;
            }
            
            queryString = queryString.substring(0, queryString.length() - 5);
        }

        if (queryElements.getOrderByColumn() != null) {
            if (!queryElements.getOrderByColumn().isEmpty()) {
                queryString += QueryBuilder.ORDER_BY + queryElements.getClassVariable() + "." + queryElements.getOrderByColumn() + " " + queryElements.getOrderByOrder();
            }
        }
        return queryString;
    }
}
